<?php
include_once '../fn.php';
loginCheck();
$flag = 'slides';

?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <title>Slides &laquo; Admin</title>
    <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="../assets/vendors/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="../assets/vendors/nprogress/nprogress.css">
    <link rel="stylesheet" href="../assets/css/admin.css">
    <script src="../assets/vendors/nprogress/nprogress.js"></script>
</head>

<body>
    <script>
        NProgress.start()
    </script>

    <div class="main">
        <nav class="navbar">
            <button class="btn btn-default navbar-btn fa fa-bars"></button>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
                <li><a href="login.php"><i class="fa fa-sign-out"></i>退出</a></li>
            </ul>
        </nav>
        <div class="container-fluid">
            <div class="page-title">
                <h1>图片轮播</h1>
            </div>
            <!-- 有错误信息时展示 -->
            <!-- <div class="alert alert-danger">
        <strong>错误！</strong>发生XXX错误
      </div> -->
            <div class="row">
                <div class="col-md-4">
                    <form id="form">
                        <h2>添加新轮播内容</h2>
                        <div class="form-group">
                            <label for="image">图片</label>
                            <!-- show when image chose -->
                            <img class="help-block thumbnail" style="display: none" id="localShow">
                            <input id="image" class="form-control" name="image" type="file">
                        </div>
                        <div class="form-group">
                            <label for="text">文本</label>
                            <input id="text" class="form-control" name="text" type="text" placeholder="文本">
                        </div>
                        <div class="form-group">
                            <label for="link">链接</label>
                            <input id="link" class="form-control" name="link" type="text" placeholder="链接">
                        </div>
                        <div class="form-group">
                            <input class="btn btn-primary" type="button" value="添加" id="addPic">
                        </div>
                    </form>
                </div>
                <div class="col-md-8">
                    <div class="page-action">
                        <!-- show when multiple checked -->
                        <a class="btn btn-danger btn-sm" href="javascript:;" style="display: none">批量删除</a>
                    </div>
                    <table class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <th class="text-center">图片</th>
                                <th>文本</th>
                                <th>链接</th>
                                <th class="text-center" width="100">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- 模板引擎填充 -->
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入公共侧边栏 -->
    <?php include './inc/aside.php'; ?>

    <script src="../assets/vendors/jquery/jquery.js"></script>
    <script src="../assets/vendors/bootstrap/js/bootstrap.js"></script>
    <script>
        NProgress.done()
    </script>
    <!-- 导入模板引擎 -->
    <script src="../assets/vendors/template/template-web.js"></script>

    <!-- 构建模板 -->
    <script type="text/html" id="tmp">

        {{ each list v i }}
        <tr>
            <td class="text-center"><img class="slide" src="../{{ v.image }}"></td>
            <td>{{ v.text }}</td>
            <td>{{ v.link }}</td>
            <td class="text-center">
                <a href="javascript:;" class="btn btn-danger btn-xs delBtn" data-index="{{ i }}">删除</a>
            </td>
        </tr>
        {{ /each }}

    </script>

    <script>

        // 封装一个渲染轮播图列表的函数
        function render() {

            // 与后台通信获取数据
            $.ajax({
                url: './slides/slidesGet.php',
                dataType: 'json',
                success: function (info) {
                    // console.log(info);
                    var data = JSON.parse(info[0].value);
                    console.log(data);
                    
                    // 调用模板引擎填充
                    $('tbody').html(template('tmp',{list:data}));
                }
            });
        }

        render();

        // ----------------------------------------------------------------------------
        // 删除功能的实现
        $('tbody').on('click','.delBtn',function () {
            console.log('删除按钮被点了');
            
            // 与后台通信
            $.ajax({
                type: 'get',
                url: './slides/slidesDel.php',
                data: {
                    index: $(this).attr('data-index')
                },
                dataType: 'json',
                success: function (info) {
                    console.log(info);
                    
                    // 重新渲染
                    render();
                }
            });
        });

        // -------------------------------------------------------------------------
        // 预览本地上传图片功能实现
        $('#image').on('change',function () {
            console.log('上传图片啦');
            // 获取上传的文件对象
            var file = this.files[0];

            if (file) {
                // 创建本地文件url
                var url = URL.createObjectURL(file);
                console.log(url);

                $('#localShow').prop('src',url).show();
            }else{
                $('#localShow').hide();
            }
        });

        // ---------------------------------------------------------------------------
        // 添加功能实现
        $('#addPic').click(function () {
            console.log('添加图片被点了');

            var fd = new FormData($('#form')[0]);
            
            // 与后台通信
            $.ajax({
                type: 'post',
                url: './slides/slidesAdd.php',
                data: fd,
                dataType: 'text',
                success: function (info) {
                    
                    // 重新渲染
                    render();
                    // 表单重置
                    $('#form')[0].reset();
                    $('#localShow').hide();
                },
                contentType: false,
                processData: false
            });
        });
    </script>

</body>

</html>